package com.byron.pss.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.byron.pss.dao.ILostFoundDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl;
import com.byron.pss.domain.LostFound;
import com.byron.pss.domain.LostFoundStr;
import com.byron.pss.util.Transform;

/**
 * 下载所有信息：唯一来源是   点击下载信息
 * @author Byron
 *
 */
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * 物品操作dao
	 */
	private ILostFoundDAO dao = new LostFoundDAOImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置响应类型为txt
		resp.setContentType("application/x-msdownload;");
		String filename = "losts.xls";
		resp.setHeader("Content-Disposition", "attachment;filename=" + filename);

		PrintWriter out = resp.getWriter();
		String title = "编号\t物品名称\t描述\t遗失位置\t遗失时间\t数量\t其它信息\r\n";
		out.print(title);
		// 查询所有的商品信息
		List<LostFoundStr> list = Transform.list0jsp(dao.list());
		StringBuilder sb;
		for (LostFoundStr lfstr:list) {
			sb = new StringBuilder();
			sb.append(lfstr.getId()).append("\t");
			sb.append(lfstr.getName()).append("\t");
			sb.append(lfstr.getDescribe()).append("\t");
			sb.append(lfstr.getLocation()).append("\t");
			sb.append(lfstr.getLostTime()).append("\t");
			sb.append(lfstr.getNumber()).append("\t");
			sb.append(lfstr.getOther()).append("\r\n");
			out.print(sb);
		}
		out.close();
	}

}
